/**
 * @description 全局过滤器
 */

const vFilter = {
  atReplace(value) {
    // 正则表达式，匹配 '@昵称:userName'
    const REG_FOR_AT_WHO = /@(.+?):(\w+?)\b/g
    const result = value.replace(REG_FOR_AT_WHO, (matchStr, nickName, userName) => {
      return `<a style="color:#2999cc" href="#/userInfo/:${userName}">@${nickName}</a>`
    })
    return result
  },
  changeTime(value) {
    // value左边那个属性值
    const t = new Date(value)
    const tf = function(i) {
      return (i < 10 ? '0' : '') + i
    }
    const format = 'yyyy-MM-dd HH:mm'
    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) {
      switch (a) {
        case 'yyyy':
          return tf(t.getFullYear())
        case 'MM':
          return tf(t.getMonth() + 1)
        case 'mm':
          return tf(t.getMinutes())
        case 'dd':
          return tf(t.getDate())
        case 'HH':
          return tf(t.getHours())
        case 'ss':
          return tf(t.getSeconds())
      }
    })
  },
  // getImage(value) {
  //   return require(`@/assets/images/icon/${value}.png`)
  // },
  getColor(name) {
    let className = ''
    switch (name) {
      case '优':
        className = 'greenColor'
        break
      case '良':
        className = 'yellowColor'
        break
      case '轻度污染':
        className = 'orangeColor'
        break
      case '中度污染':
        className = 'redColor'
        break
      case '重度污染':
        className = 'properColor'
        break
      case '严重污染':
        className = 'deepRedColor'
        break
      default:
        className = 'garyColor'
        break
    }
    return className
  },
  getClassColor(level, check) {
    let className = ''
    switch (level) {
      case '优':
        className = check ? 'aqiTd1' : 'aqiTd1-tran'
        break
      case '良':
        className = check ? 'aqiTd2' : 'aqiTd2-tran'
        break
      case '轻度污染':
        className = check ? 'aqiTd3' : 'aqiTd3-tran'
        break
      case '中度污染':
        className = check ? 'aqiTd4' : 'aqiTd4-tran'
        break
      case '重度污染':
        className = check ? 'aqiTd5' : 'aqiTd5-tran'
        break
      case '严重污染':
        className = check ? 'aqiTd6' : 'aqiTd6-tran'
        break
      default:
        className = check ? 'aqiTd0' : 'aqiTd0-tran'
        break
    }
    return className
  },
  parseTime(time, cFormat) {
    if (arguments.length === 0 || !time) {
      return null
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
      date = time
    } else {
      if (typeof time === 'string') {
        // 兼容处理
        if (/^[0-9]+$/.test(time)) {
          time = parseInt(time)
        } else {
          time = time.replace(new RegExp(/-/gm), '/')
        }
      }

      if (typeof time === 'number' && time.toString().length === 10) {
        time = time * 1000
      }
      date = new Date(time)
    }
    const formatObj = {
      y: date.getFullYear(),
      m: date.getMonth() + 1,
      d: date.getDate(),
      h: date.getHours(),
      i: date.getMinutes(),
      s: date.getSeconds(),
      a: date.getDay()
    }
    const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
      const value = formatObj[key]
      if (key === 'a') {
        return ['日', '一', '二', '三', '四', '五', '六'][value]
      }
      return value.toString().padStart(2, '0')
    })
    return time_str
  },
  formatTime(time, option) {
    if (('' + time).length === 10) {
      time = parseInt(time) * 1000
    }
    const d = +new Date(time).getTime()
    const now = Date.now()

    const diff = (now - d) / 1000

    if (diff < 30) {
      return '刚刚'
    } else if (diff < 3600) {
      return Math.ceil(diff / 60) + '分钟前'
    } else if (diff < 3600 * 24) {
      return Math.ceil(diff / 3600) + '小时前'
    } else if (diff < 3600 * 24 * 2) {
      return '1天前'
    }
    if (option) {
      return vFilter.parseTime(time, option)
    } else {
      return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
    }
  },
  formatType(data) {
    let Name = ''
    switch (data) {
      case 'WRDK_COMPANY':
        Name = '污染地块'
        break
      case 'WATER_SECTION':
        Name = '考核断面'
        break
      case 'POLLUTION_COMPANY':
        Name = '工业污染源'
        break
    }
    return Name
  },
  getRankColor(rank) {
    let className
    switch (rank) {
      case '1':
        className = 'bg1'
        break
      case '2':
        className = 'bg2'
        break
      case '3':
        className = 'bg3'
        break
      default:
        className = ''
        break
    }
    return className
  },
  getNumberToLM(data) {
    let text
    switch (Number(data)) {
      case 1:
        text = 'Ⅰ'
        break
      case 2:
        text = 'Ⅱ'
        break
      case 3:
        text = 'Ⅲ'
        break
      case 4:
        text = 'Ⅳ'
        break
      case 5:
        text = 'Ⅴ'
        break
      case 6:
        text = '劣Ⅴ'
        break
    }
    return text
  }
}

export default vFilter
